package com.feng.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.feng.common.utils.Asserts;
import com.feng.mbg.domain.SysPermission;
import com.feng.mbg.domain.SysPermissionExample;
import com.feng.mbg.mapper.SysPermissionMapper;
import com.feng.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author Feng
 * @since 2022/4/20 18:26
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    SysPermissionMapper permissionMapper;

    @Override
    public List<SysPermission> getAllPermissions() {
        SysPermissionExample example = new SysPermissionExample();
        example.createCriteria().andIsDeletedEqualTo(0);
        return permissionMapper.selectByExample(example);
    }

    @Override
    public void addPermission(String name, String key) {
        if (getByName(name) != null) {
            Asserts.fail("权限名为" + name + "的权限已存在");
        }
        if (getByKey(key) != null) {
            Asserts.fail("权限key为" + key + "的权限已存在");
        }
        SysPermission permission = new SysPermission();
        permission.setPermissionName(name);
        permission.setPermissionKey(key);
        permissionMapper.insertSelective(permission);
    }

    @Override
    public void updatePermission(Long id, String name, String key) {
        SysPermission permission = permissionMapper.selectByPrimaryKey(id);
        if (permission == null || permission.getIsDeleted().equals(1)) {
            Asserts.fail("ID为" + id + "的权限不存在");
        }
        if (!StrUtil.equals(name, permission.getPermissionName()) && getByName(name) != null) {
            Asserts.fail("权限名为" + name + "的权限已存在");
        }
        if (!StrUtil.equals(key, permission.getPermissionKey()) && getByKey(key) != null) {
            Asserts.fail("权限key为" + key + "的权限已存在");
        }
        permission.setPermissionName(name);
        permission.setPermissionKey(key);
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public void deletePermissionByIds(Set<Long> ids) {
        SysPermissionExample example = new SysPermissionExample();
        example.createCriteria().andPermissionIdIn(new ArrayList<>(ids));
        SysPermission permission = new SysPermission();
        permission.setIsDeleted(1);
        permissionMapper.updateByExampleSelective(permission, example);
    }

    private SysPermission getByName(String name) {
        SysPermissionExample example = new SysPermissionExample();
        example.createCriteria().andPermissionNameEqualTo(name).andIsDeletedEqualTo(0);
        List<SysPermission> permissions = permissionMapper.selectByExample(example);
        if (CollectionUtil.isEmpty(permissions)) {
            return null;
        }
        return permissions.get(0);
    }

    private SysPermission getByKey(String key) {
        SysPermissionExample example = new SysPermissionExample();
        example.createCriteria().andPermissionKeyEqualTo(key).andIsDeletedEqualTo(0);
        List<SysPermission> permissions = permissionMapper.selectByExample(example);
        if (CollectionUtil.isEmpty(permissions)) {
            return null;
        }
        return permissions.get(0);
    }
}
